1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.collect.testing.google;
16  
17  import static com.google.common.collect.testing.Helpers.copyToList;
18  import static com.google.common.collect.testing.Helpers.mapEntry;
19  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
20  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
21  import static com.google.common.truth.Truth.assertThat;
22  
23  import com.google.common.annotations.GwtCompatible;
24  import com.google.common.collect.ListMultimap;
25  import com.google.common.collect.testing.features.CollectionSize;
26  import com.google.common.collect.testing.features.MapFeature;
27  
28  import java.util.Arrays;
29  import java.util.Collection;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * Testers for {@link ListMultimap#remove(Object, Object)}.
35   *
36   * @author Louis Wasserman
37   */
38  @GwtCompatible
39  public class ListMultimapRemoveTester<K, V> extends AbstractListMultimapTester<K, V> {
40    @SuppressWarnings("unchecked")
41    @MapFeature.Require(SUPPORTS_REMOVE)
42    @CollectionSize.Require(SEVERAL)
43    public void testMultimapRemoveDeletesFirstOccurrence() {
44      K k = sampleKeys().e0;
45      V v0 = sampleValues().e0;
46      V v1 = sampleValues().e2;
47      resetContainer(mapEntry(k, v0), mapEntry(k, v1), mapEntry(k, v0));
48  
49      List<V> list = multimap().get(k);
50      multimap().remove(k, v0);
51      assertThat(list).has().exactly(v1, v0).inOrder();
52    }
53  
54    @SuppressWarnings("unchecked")
55    @MapFeature.Require(SUPPORTS_REMOVE)
56    @CollectionSize.Require(SEVERAL)
57    public void testRemoveAtIndexFromGetPropagates() {
58      K k = sampleKeys().e0;
59      V v0 = sampleValues().e0;
60      V v1 = sampleValues().e2;
61      List<V> values = Arrays.asList(v0, v1, v0);
62  
63      for (int i = 0; i < 3; i++) {
64        resetContainer(mapEntry(k, v0), mapEntry(k, v1), mapEntry(k, v0));
65        List<V> expectedValues = copyToList(values);
66  
67        multimap().get(k).remove(i);
68        expectedValues.remove(i);
69  
70        assertGet(k, expectedValues);
71      }
72    }
73  
74    @SuppressWarnings("unchecked")
75    @MapFeature.Require(SUPPORTS_REMOVE)
76    @CollectionSize.Require(SEVERAL)
77    public void testRemoveAtIndexFromAsMapPropagates() {
78      K k = sampleKeys().e0;
79      V v0 = sampleValues().e0;
80      V v1 = sampleValues().e2;
81      List<V> values = Arrays.asList(v0, v1, v0);
82  
83      for (int i = 0; i < 3; i++) {
84        resetContainer(mapEntry(k, v0), mapEntry(k, v1), mapEntry(k, v0));
85        List<V> expectedValues = copyToList(values);
86  
87        List<V> asMapValue = (List<V>) multimap().asMap().get(k);
88        asMapValue.remove(i);
89        expectedValues.remove(i);
90  
91        assertGet(k, expectedValues);
92      }
93    }
94  
95    @SuppressWarnings("unchecked")
96    @MapFeature.Require(SUPPORTS_REMOVE)
97    @CollectionSize.Require(SEVERAL)
98    public void testRemoveAtIndexFromAsMapEntrySetPropagates() {
99      K k = sampleKeys().e0;
100     V v0 = sampleValues().e0;
101     V v1 = sampleValues().e2;
102     List<V> values = Arrays.asList(v0, v1, v0);
103 
104     for (int i = 0; i < 3; i++) {
105       resetContainer(mapEntry(k, v0), mapEntry(k, v1), mapEntry(k, v0));
106       List<V> expectedValues = copyToList(values);
107 
108       Map.Entry<K, Collection<V>> asMapEntry = multimap().asMap().entrySet().iterator().next();
109       List<V> asMapValue = (List<V>) asMapEntry.getValue();
110       asMapValue.remove(i);
111       expectedValues.remove(i);
112 
113       assertGet(k, expectedValues);
114     }
115   }
116 }